package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmChannelMapper wmChannelMapper;
    @Autowired
    private WmNewsMapper wmNewsMapper;

    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 频道模糊查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(ChannelDto dto) {
        //检查参数
        dto.checkParam();
        //2.分页查询
        IPage page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据名称模糊查询
        if (StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(WmChannel::getName, dto.getName());
        }
        //根据状态查询
        if (dto.getStatus()!=null){
            lambdaQueryWrapper.eq(WmChannel::getStatus,dto.getStatus());
        }
        //根据创建时间倒序查询
        lambdaQueryWrapper.orderByDesc(WmChannel::getCreatedTime);
        page = page(page,lambdaQueryWrapper);
        //结果返回
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());

        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteChannelById(Integer id) {
        WmChannel wmChannel = wmChannelMapper.selectById(id);
        if (wmChannel.getStatus().equals(true)){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_CITED);
        }
        wmChannelMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 新增频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        WmChannel channel = new WmChannel();
        if (wmChannel.getId() == null){
            WmChannel selectOne = wmChannelMapper.selectOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));
            //频道名称不能重复
            if (selectOne != null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名称不能重复");
            }
            channel.setName(wmChannel.getName());
            channel.setCreatedTime(new Date());
            channel.setDescription(wmChannel.getDescription());
            channel.setOrd(wmChannel.getOrd());
            channel.setStatus(wmChannel.getStatus());
            channel.setIsDefault(true);
            wmChannelMapper.insert(channel);
        }
        return ResponseResult.okResult(wmChannel.getId());
    }

    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        WmNews wmNews = wmNewsMapper.selectOne(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, wmChannel.getId()));
        if (wmNews != null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_CITED);
        }
        wmChannelMapper.updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}